package com.cpr.core.rabbitmq.client.topic;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.DeliverCallback;
import com.rabbitmq.client.Delivery;

import java.io.IOException;

/**
 * @author Chen Panrui
 * created at 2022/3/15 22:15
 */
public class ConsumerShanghaiError {
    public static void main(String[] args) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        // 指定协议 用户名+密码+host+端口号+虚拟主机
        factory.setUri("amqp://admin:123456@106.13.89.7:5672/%2f");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        // 临时队列
        String queue = channel.queueDeclare().getQueue();
        channel.exchangeDeclare("ex.topic", BuiltinExchangeType.TOPIC, true, false, null);
        channel.queueBind(queue, "ex.topic", "shanghai.*.error");

        channel.basicConsume(queue, new DeliverCallback() {
            @Override
            public void handle(String consumerTag, Delivery message) throws IOException {
                System.out.println("shanghai.*.error-接收到的消息:" + new String(message.getBody()));
            }
        }, new CancelCallback() {
            @Override
            public void handle(String consumerTag) throws IOException {

            }
        });
    }
}
